package com.sky.controller.admin;


import com.google.j2objc.annotations.AutoreleasePool;
import com.sky.mapper.OrderMapper;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Api(tags = "营业额统计接口")
@RestController
@Slf4j
@RequestMapping("/admin/report")
public class ReportController {


    @Autowired
    OrderService orderService;

    @Autowired
    ReportService reportService;


    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation("营业额统计")
    @GetMapping("/turnoverStatistics")
    public Result turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){

//        log.info("开始时间："+begin);
//        log.info("结束时间："+end);

        log.info("开始时间：{}",begin);
        log.info("结束时间：{}",end);

        TurnoverReportVO turnoverReportVO = orderService.turnoverStatistics(begin, end);
        return Result.success(turnoverReportVO);
    }


    @ApiOperation("用户统计")
    @GetMapping("/userStatistics")
    public Result userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("开始时间：{}",begin);
        log.info("结束时间：{}",end);

        UserReportVO userReportVO = orderService.userStatistics(begin, end);
        log.info( "用户统计参数："+userReportVO);

        return  Result.success(userReportVO);

    }



    @ApiOperation("订单统计")
    @GetMapping("/ordersStatistics")
    public Result ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("开始时间：{}",begin);
        log.info("结束时间：{}",end);

        OrderReportVO orderReportVO = orderService.ordersStatistics(begin, end);

        return  Result.success(orderReportVO);

    }

    @GetMapping("/top10")
    public Result top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){

        log.info("开始时间：{}",begin);
        log.info("结束时间：{}",end);
        SalesTop10ReportVO salesTop10ReportVO = orderService.top10(begin, end);
        return Result.success(salesTop10ReportVO);
    }


    @GetMapping("/export")
    @ApiOperation("导出运营数据报表")
    public void export(HttpServletResponse response){

            reportService.report(response);
    }
}
